from MySpider import mydb
import asyncio

from scrapy.http import HtmlResponse
import tkinter as tk
from tkinter import ttk
from tkinter import Label, Entry, Button, E, W, N, S, LEFT
from playwright.async_api import async_playwright


class XpathTool:
    def __init__(self, root):
        self.root = root
        self.root.title = 'xpath自制小工具'
        self.root.geometry("%dx%d" % (root.winfo_screenwidth(), root.winfo_screenheight()))

        self.db_history_tree = ttk.Treeview(root, columns=("id", "domain", "url", "xpath", "result"), height=5)
        self.db_history_tree.heading("id", text="序号")
        self.db_history_tree.heading("domain", text="接受域")
        self.db_history_tree.heading("url", text="网址")
        self.db_history_tree.heading("xpath", text="xpath")
        self.db_history_tree.heading("result", text="提取字段")

        self.result_tree = ttk.Treeview(root, columns=("序号", "元素"), name='result_tree', height=5)

        self.domain_label = Label(root, text='domain:')
        self.domain_entry = Entry(root, name='domain_entry', width=15)
        self.url_label = Label(root, text='网址:')
        self.url_entry = Entry(root, name='url_entry', width=15)
        self.xpath_label = Label(root, text='xpath:')
        self.xpath_entry = Entry(root, name='xpath_entry', width=15)
        self.button_gen_xpath_result = Button(root, text="测试xpath", command=self.on_gen_xpath_result_click)
        self.button_save_db = Button(root, text="导入数据库", command=self.on_save_db_click)

        self.domain_label.grid(row=0, column=0, sticky=E)
        self.domain_entry.grid(row=0, column=1, sticky=W + E + N + S)
        self.url_label.grid(row=0, column=2, sticky=E)
        self.url_entry.grid(row=0, column=3, sticky=W + E + N + S)
        self.xpath_label.grid(row=1, column=0, sticky=E)
        self.xpath_entry.grid(row=1, column=1, columnspan=3, sticky=W + E + N + S)

        self.button_gen_xpath_result.grid(row=2, column=0, sticky=E)
        self.button_save_db.grid(row=2, column=1, sticky=W)
        self.result_tree.grid(row=3, column=0, columnspan=4, sticky=W + E + N + S)
        root.columnconfigure(1, weight=1)
        root.columnconfigure(3, weight=1)

        self.data = mydb.get_xpath()
        for d in self.data:
            self.db_history_tree.insert("", tk.END, values=(d[0], d[1], d[2], d[3], d[4]))
        self.db_history_tree.grid(row=4, column=0, columnspan=6, sticky=W + E + N + S)

    def on_save_db_click(self):
        all_widget = self.button_save_db.master.children
        domain_entry = all_widget['domain_entry']
        url_entry = all_widget['url_entry']
        xpath_entry = all_widget['xpath_entry']
        result_tree = all_widget['result_tree']

        mydb.save_xpath(domain_entry.get(), url_entry.get(), xpath_entry.get(), result_tree.cget("text"))

    async def on_gen_xpath_result_click(self):
        all_widget = self.button_gen_xpath_result.master.children
        async with async_playwright() as p:
            browser = await p.chromium.launch()
            page = await browser.new_page()
            url = all_widget['url_entry'].get()
            await page.goto(url)
        body = page.content()
        # 伪造一个response，以上可以是任何方式获得的网页源代码
        response = HtmlResponse(url=url, body=body, encoding="utf-8")
        elements = response.xpath(all_widget['xpath_entry'].get()).extract()

        rt = all_widget['result_tree']
        result = ''
        # 打印获取到的元素内容
        i = 1
        for ele in elements:
            rt.insert("", tk.END, values=(i, ele))
            i += 1


async def main():
    root = tk.Tk()
    XpathTool(root)
    root.mainloop()


if __name__ == "__main__":
    asyncio.run(main())
